package service;

import dao.ChatListDao;
import dao.ChatRecordDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.ChatList;
import pojo.form.ChatListAddForm;
import pojo.form.ChatRecordAddForm;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by asus on 2018/11/22.
 */
@Service
public class ChatAopService {
    public Logger logger = LoggerFactory.getLogger(ChatAopService.class);
    ThreadPoolExecutor threadPools;

    @Autowired
    ChatRecordDao charRecordDao;
    @Autowired
    ChatListDao dao;
    @Autowired
    public ChatAopService(@Value("#{config['AsyncFramework.minThreadPoolSize']}") int minThreadPoolSize,
                          @Value("#{config['AsyncFramework.maxThreadPoolSize']}") int maxThreadSize,
                          @Value("#{config['AsyncFramework.threadKeepAliveTime']}") long getThreadKeepAliveTime) {
        threadPools = new ThreadPoolExecutor(minThreadPoolSize,
                maxThreadSize,
                getThreadKeepAliveTime, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    /**
     * 保存聊天记录
     *
     * @param
     */
    public void addChatRecord(final ChatRecordAddForm addForm) {
        this.threadPools.execute(new Runnable() {
            public void run() {
                charRecordDao.addCharRecord(addForm);
            }
        });
    }
    public void updateChatList(final String message, final Long shopId, final Long customId,final ChatListAddForm chatListAddForm){
        this.threadPools.execute(new Runnable() {
            @Override
            public void run() {
                dao.addCharRecordExist(chatListAddForm);
                dao.updateChatList(message,shopId,customId);
            }
        });
    }

    public void addCharRecordExist(final ChatListAddForm addForm){
        this.threadPools.execute(new Runnable() {
            @Override
            public void run() {
                 dao.addCharRecordExist(addForm);
            }
        });

    }
}
